#pragma once

#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Inet_Addr.hpp"

enum error
{
    success = 1,
    fail,
    err
};
const int SIZE = 1024;
const int BACKLOG = 8;

class TcpServer
{
private:
    socklen_t _sockfd; // socket fd --- create socket
    std::string _ip;
    uint16_t _port;

    bool is_running;

public:
    // ./server 8899
    TcpServer(uint16_t port)
        : _port(port)
    {
        _sockfd = -1;
        _ip = "0";
        is_running = false;
    }
    void Init()
    {
        // create socket
        _sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (_sockfd < 0)
        {
            std::cout << "tcp socket create fail" << std::endl;
            exit(fail);
        }
        std::cout << "tcp socket create success" << std::endl;

        // bind socket
        sockaddr_in local;
        local.sin_family = AF_INET;
        local.sin_port = htons(_port);
        local.sin_addr.s_addr = inet_addr(_ip.c_str());
        int bind_n = bind(_sockfd, (struct sockaddr *)&local, sizeof(local));
        if (bind_n < 0)
        {
            std::cout << "tcp socket bind fail" << std::endl;
            exit(fail);
        }
        std::cout << "tcp socket bind success" << std::endl;

        // listen
        int listen_n = listen(_sockfd, BACKLOG);
        if (listen_n < 0)
        {
            std::cout << "tcp socket listen fail" << std::endl;
            exit(fail);
        }
        std::cout << "tcp socket listen success" << std::endl;

    }
    void Start()
    {
        is_running = true;
        while (is_running)
        {
            // accept
            sockaddr_in com_addr;
            socklen_t len = sizeof(com_addr);
            // comfd --- communication fd ---> communicate
            int comfd = accept(_sockfd, (struct sockaddr *)&com_addr, &len);
            if (comfd < 0)
            {
                std::cout << "tcp socket accept fail" << std::endl;
            }
            std::cout << "tcp socket accept success" << std::endl;
            Inet_Addr addr(com_addr);
            service(comfd, addr);
        }
    }
    void service(int comfd, Inet_Addr addr)
    {
        while (true)
        {
            // communication --- string_stream
            // read
            char read_message[SIZE] = {0};
            int read_n = read(comfd, read_message, sizeof(read_message) - 1);
            if (read_n < 0)
            {
                std::cout << "read fail" << std::endl;
                exit(fail);
            }
            else if (read_n == 0)
            {
                std::cout << "read end" << std::endl;
                exit(success);
            }
            std::cout << "read success: " << read_message << std::endl;

            // write --- return read_message
            std::string echo_message = "[echo message] #";
            echo_message += read_message;
            int write_n = write(comfd, read_message, read_n);
            if (write_n < 0)
            {
                std::cout << "write fail" << std::endl;
                exit(fail);
            }
            std::cout << "write success!" << std::endl;
        }
    }
    ~TcpServer() 
    {
        if (_sockfd > 0)
        {
            close(_sockfd);
        }
    }
};
